import sys
import json5
import json
PACKAGE_JSON = 'package.json'
CUSTOM_DEPENDENCIES = 'geelyDependencies'


def changePackage(projectPath, componentName, componentVersion, description, author):
    print(f"修改文件地址:{projectPath}{PACKAGE_JSON}")
    with open(f"{projectPath}{PACKAGE_JSON}", 'r', encoding='utf-8') as f:
        data = json5.load(f)
    print(data)
    if not componentName is None:
        data['name'] = componentName

    if not componentVersion is None:
        data['version'] = componentVersion

    if not description is None:
        data['description'] = description

    if not description is None:
        data['description'] = description

    if not author is None:
        data['author'] = author

    with open(f"{projectPath}{PACKAGE_JSON}", 'w', encoding='utf-8') as f:
        json.dump(data, f, indent=2, ensure_ascii=False)


def getDependencies(projectPath):
    with open(f"{projectPath}{PACKAGE_JSON}", 'r', encoding='utf-8') as f:
        data = json5.load(f)
        if CUSTOM_DEPENDENCIES in data:
             print(f"{data[CUSTOM_DEPENDENCIES]}")
        else:
            print("无依赖库")

def addFileConfigContent(file_path,config_content):
    try:
        with open(f'{file_path}', 'r', encoding='utf-8') as f:
            content = f.read()
            if config_content in content:
                print(f"已存在:{config_content}")
            else:
                # 修改配置文件
                with open(f'{file_path}', 'a', encoding='utf-8') as f:
                    f.write("\n")
                    f.write(f"{config_content}")
    except Exception as e:
        print(f"读取file_content失败：{e}")

    print(f"file_path： {file_path}")

    try:
        with open(f'{file_path}', 'r', encoding='utf-8') as f:
            content = f.read()
            print(f"file_content:{content}")
    except Exception as e:
        print(f"读取file_content失败：{e}")

def printHelp(command):
    if command == '-c':
        print("-c: 修改前端组件信息：projectPath, component_name, component_version, description, author")
        print( "ComponentBasicInfo.py -t '{projectPath}' '{component_name}' '{component_version}' '{description}' '{author}' ")
        exit(0)
    elif command == '-g':
        print("-g: 获取前端组件依赖库：projectPath")
        print( "ComponentBasicInfo.py -n '{projectPath}' ")
        exit(0)
    else:
        print("Use: ComponentBasicInfo.py -[c|g|a]")
        print("-c: 修改前端组件信息：projectPath, component_name, component_version, description, author")
        print("ComponentBasicInfo.py -c '{projectPath}' '{component_name}' '{component_version}' '{description}' '{author}' ")

        print("-g: 获取前端组件依赖库：projectPath")
        print("ComponentBasicInfo.py -g '{projectPath}' ")

        print("-a: 构建脚本新增配置：filePath, config_content")
        print("ComponentBasicInfo.py -a '{filePath} '{config_content}' ")
        exit(0)

if __name__ == "__main__":
    # changePackage('/Users/gymd/DevEcoStudioProjects/hm-demo/workspace/vue/', '名称', '0.0.1', '我是描述1231', 'e-zhaihao')
    # getDependencies('/Users/gymd/DevEcoStudioProjects/hm-demo/workspace/vue/')

    if len(sys.argv) < 2:
        printHelp('error')
    command = sys.argv[1]
    print(f"参数长度：{len(sys.argv)}{sys.argv[0]}{sys.argv}")
    if command == "-c":
        if len(sys.argv) < 7:
            printHelp(command)
        changePackage(sys.argv[2], sys.argv[3], sys.argv[4], sys.argv[5], sys.argv[6])
    elif command == "-g":
        if len(sys.argv) < 3:
            printHelp(command)
        getDependencies(sys.argv[2])
    elif command == "-a":
        if len(sys.argv) < 4:
            printHelp(command)
        addFileConfigContent(sys.argv[2], sys.argv[3])
    else:
        printHelp('error')

# python ComponentBasicInfo.py -c '/Users/gymd/DevEcoStudioProjects/hm-demo/workspace/vue/' '名称' '0.0.1' '我是描述' 'e-zh'
# python ComponentBasicInfo.py -g '/Users/gymd/DevEcoStudioProjects/hm-demo/workspace/vue/'